<%@ page pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<jsp:include page="admin/common/Userheader.jsp"></jsp:include>
<script src="${pageContext.request.contextPath}/views/js/libs/jquery-1.12.4.js" type="text/javascript"></script>
<link href="${pageContext.request.contextPath}/views/js/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/views/js/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />

<script src="${pageContext.request.contextPath}/views/js/ligerUI/js/core/base.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/views/js/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/views/js/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/views/js/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/views/js/utils.js" type="text/javascript"></script> 
<!DOCTYPE html>
<html lang="zh-CN" style="overflow-x:hidden; width: 100%;">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap Template</title>
    
    <!-- Bootstrap -->
    <link href="${pageContext.request.contextPath}/views/css/bootstrap.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="${pageContext.request.contextPath}/views/js/html5shiv.min.js"></script>
      <script src="${pageContext.request.contextPath}/views/js/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="${pageContext.request.contextPath}/views/js/common/common.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/views/js/utils.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/views/css/home.css">
    <style>

    </style>
</head>

<body>
<!--弹窗开始-->
<div class="add_mask"></div>
<div class="add_mask_content">
	<ul>
	    <li class="name"><label for="add_name">用户名<i>*</i></label><input type="text" id="add_name"></li>
	    <li class="branch"><label for="add_branch">所在部门<i>*</i></label><input type="text" id="add_branch"><a href="javascript:;">所在部门</a></li>
	    <li class="tel"><label for="add_tel">手机号</label><input type="text" id="add_tel"></li>
	    <li class="user">
	    	<label>角色<i>*</i></label>
	    	<select>
	    		<option>角色1</option>
	    		<option>角色2</option>
	    	</select>
	    </li>
	    <li class="state"><label>状态<i>*</i></label><input class="radio" type="radio" name="radio01"><span>启用</span><input class="radio" type="radio" name="radio01"><span>停用</span></li>
	</ul>
	<button>确定</button>
</div>
<div class="add_mask_content add_mask_branch">
	<div>
		<h1>选择部门</h1>
		<a href="javascript:;">X</a>
	</div>
</div>
<!--弹窗结束-->

    <input type="hidden" id="orderid1" value="asc"/>
    <input type="hidden" id="orderid2" value="asc"/>
    <input type="hidden" id="orderid3" value="asc"/>
    <input type="hidden" id="orderid4" value="asc"/>
    <input type="hidden" id="orderid5" value="asc"/>
    <input type="hidden" id="pageIndex" />
    <input type="hidden" id="seldepartid" value="${selDepartId}"/>
<!--     上级文件夹id -->
    <input type="hidden" id="upid" value="${upid }" />
    
    <div class="tab_content" style="${two}">
    	<a href="javascript:;" style="${folder}" class="current"  onclick="foldermanage();">文件管理</a>
    	<a href="javascript:;" style="${users}" onclick="usersmanage();">用户管理</a>
    </div>
    
    <!--右侧内容区-->
            <div class="pull-left right-components" style="width: 100%; margin-left: 0">
                <!--右侧顶部-->
                <div class="right-header-component clearfix tool">
                    <div class="pull-left" style="margin-left:20px;">
                        <button type="button" id="createfolderbut" class="btn btn-default dropdown-toggle" aria-haspopup="true" onclick="add();">
                            	新建用户
                        </button>
                        <button type="button" id="upfilebut" class="btn btn-danger btn_nobg" onclick="del();">
                            	删除用户
                        </button>
                    </div>
                    <div class="pull-right">
                        <div class="pull-left right-search-component">
                            <input type="text" id="search_con" onkeyup="onSearch();" placeholder="用户名/手机号"/>
                            <i id="right_search_btn" onclick="BindData();"></i>
                        </div>
                    </div>
                </div>

                <!--右侧内容区-->
                <div class="form-component">

                    <div class="panel panel-default">
                        <!-- 路径导航 -->
                        <ol class="breadcrumb" id="breadcrumb">
                        </ol>

                        <!-- Table -->
                        <table border="0" cellspacing="0" cellpadding="0" id="jgcsTable" class="usernanage">
                        	<thead>
	                            <tr>
	                                <th><input type="checkbox" class="checkall" id="checkall"/></th>
				                    <th>序号</th>
				                    <th>用户名</th>
				                    <th>手机号</th>
				                    <th>所属角色</th>
				                    <th>所属部门</th>
				                    <th>最后登录时间</th>
				                    <th>状态</th>
				                    <th>操作</th>
	                            </tr>
                           </thead>
				            <tbody id="tb_body">
				            </tbody>      
                        </table>
						<div id="kkpager"><!-- 分页容器 -->
        
        				</div>
                        
                    </div>
                </div>
            </div>
</body>
<script type="text/javascript">

var pageIndex = 1; // 页索引

$(function() {
	BindData(1);
	$('#checkall').change(function(){
		$(this).parents('table:first').find('input').prop('checked', $(this).is(':checked')); 
    });
})
//AJAX方法取得数据并显示到页面上
var data = [];
function BindData(pageIndex) {
	/* var status = $('#statusSel').val();
	var departName = $('#departName').val();
	var orderStr = getOrderColum(); */
	$.ajax({
		type : "post", // 使用get方法访问后台
		dataType : "json", // 返回json格式的数据
		url : "${pageContext.request.contextPath}/user/users", // 要访问的后台地址
		data : {
			"page" : pageIndex,
			"size":20,
			"username":$("#search_con").val(),
			"selDepartId":"${selDepartId}",
			"selDepartLevel":"${selDepartLevel}"
		}, // 要发送的数据
		success : function(msg) {// msg为返回的数据，在这里做数据绑定
			data = msg.data;//列表数据
			var curPageIndex=msg.curPage;//当前页码
			var total=msg.total;//数据总条数
			var totalPage=msg.totalPage;//页面总数
			$("#total").text(total);
			$("#tb_body").empty(); 
			var ordNo = (curPageIndex-1)*20;//序号
			$.each(data,function(i, item) {
				ordNo++;
				var dateVal = "";
				if (item.lastLoginTime != null
						&& item.lastLoginTime != "") {
					var dateV = new Date();
					dateV.setTime(item.lastLoginTime);
					dateVal = dateV.Format('yyyy-MM-dd hh:mm:ss');

				}
				var status = "";
				if(item.status == 1){
					status = "启用";
				}else{
					status = "禁用";
				}
				
				var owndepart = "";
				if(item.department){
					owndepart = item.department.title;
				}else{
					owndepart = "超级管理员";
				}
				
				var role_str = "";
				if(item.role==0){
					role_str = "普通用户";
				}
				if(item.role==1){
					role_str = "部门管理员";
				}
				if(item.role==3){
					role_str = "超级管理员";
				}
				
					
				$("#jgcsTable").append(
						"<tr><td><input type='checkbox' name='userId' value='"+item.id+"'/></td><td style='display:none;'>" +
					+ item.id
					+ "</td><td>"
					+ ordNo
					+ "</td><td>"
					+ item.username
					+ "</td><td>"
					+ item.mobile
					+ "</td><td >"
					+ role_str
					+ " </td><td>"
					+ owndepart
					+ " </td><td>"
					+ dateVal
					+ " </td><td>"
					+ status
					+ " </td><td width='100'>"
					+ " <a href='javascript:;' onclick='edit("+item.id+")' title='修改信息'>"
					+ "<img src='${pageContext.request.contextPath}/views/images/icons/actions/edit.png' alt='查看详细信息' id='btnInsert'style='border-width:0px;' /></a>"
					+ "<a href='javascript:;' onclick='resetpwd("+i+")' title='点击重置密码'>"
					+ "<img src='${pageContext.request.contextPath}/views/images/icons/actions/key.png' alt='重置密码'"
					+ "id='paswreset'style='border-width:0px;' /></a>" +
							"</td></tr>");
			});
			generPageBar("BindData", curPageIndex, totalPage, total);
		},
		error : function() {
			var t = document.getElementById("tb_body"); // 获取展示数据的表格
			while (t.rows.length != 0) {
				t.removeChild(t.rows[0]); // 在读取数据时如果表格已存在行．一律删除
			}
			myAlert("加载数据失败");
		} // 加载失败，请求错误处理
		// ajaxStop:$("#load").hide()
	});
}

function add(){
	saveoredit("添加用户","");
}

function edit(uid){
	saveoredit("修改用户",uid);
}

function saveoredit(tip,uid){
	$.ligerDialog.open({
	    height:500,
	    width: 700,
	    title : tip,
	    type:"post",
	    url: encodeURI('${pageContext.request.contextPath}/user/saveoredituser?uid='+uid+'&selDepartId=${selDepartId}&selDepartLevel=${selDepartLevel}'), 
	    showMax: false,
	    showToggle: true,
	    showMin: false,
	    isResize: true,
	    slide: false,
	    closeable:false,
	    data: {
	    	
	    }
	});
}


function onSearch(){
	if(event.keyCode==13){
		BindData(pageIndex);
	}
	
}

function del(){
	var ids = '';
	$("input[name='userId']:checked").each(function(){
		ids+=$(this).val()+',';
	});
	if(ids.length < 1){
		operationTips('请选择用户！');
		return;
	}
	$.ligerDialog.confirm('确认删除用户？', function (yes) {
		if(yes==true){
			delUsers(ids); 
		}	
	});
}

function delUsers(ids){
	$.ajax({
		url : "${pageContext.request.contextPath}/user/delusers",
		type : "POST",
		dataType : "json",
		data : {
			"uids" : ids,
			"_":new Date().getTime()
		},
		success : function(rtn) {
			if (rtn.code == 0) {
				operationTips('删除成功！');
				BindData(1);
			} else {
				operationTips('删除失败！');
			}
		}
	})
}

//重置密码
function resetpwd(i){
	$.ligerDialog.confirm('确认重置<span color="blue">'+data[i].username+'</span>的密码？', function (yes) {
		if(yes==true){
			$.ajax({
				url : '${pageContext.request.contextPath}/user/resetpwd',
				type : "POST",
				dataType : "json",
				data : {
					"id" : data[i].id
				},
				success : function(rtn) {
					if (rtn.code == 0) {
						operationTips('重置成功！');
						BindData(1);
					} else {
						operationTips('重置失败！');
					}
				}
			})
		}	
	});
}

//提示
function operationTips(content){
	$.ligerDialog.alert(content);
}

function reload(){
	var pageIndex = $(".currPageNum").text();
	BindData(pageIndex);
}


//文件管理
function foldermanage(){
	var selDepartId = '${selDepartId}';
	var selDepartName = '${selDepartName}';
	//window.location.href = encodeURI('index_rightonepage?selDepartId='+selDepartId
	//		+ "&selDepartName="+selDepartName);
	window.location.href = encodeURI('${pageContext.request.contextPath}/file/index_rightonepage?selDepartId=${selDepartId}'
			+ "&selDepartName=${selDepartName}");
}
//部门人员管理
function usersmanage(){
	var selDepartId = '${selDepartId}';
	window.location.href = encodeURI('${pageContext.request.contextPath}/user/usermanage?selDepartId='
			+ selDepartId+"&selDepartName=${selDepartName}");
}

</script>

</html>